﻿using System;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Data;
using YiSha.Data.Repository;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;

namespace YiSha.Service.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2023-03-19 19:32
    /// 描 述：骑行用户地址管理服务类
    /// </summary>
    public class CustomerAddrService :  RepositoryFactory
    {
        #region 获取数据
        public async Task<List<CustomerAddrEntity>> GetList(CustomerAddrListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.ToList();
        }

        public async Task<List<CustomerAddrEntity>> GetPageList(CustomerAddrListParam param, Pagination pagination)
        {
            StringBuilder strSql = new StringBuilder();
            var parameter = new List<DbParameter>();
            strSql.Append(@"SELECT a.*,b.nickname FROM customer_addr  a JOIN
            customer_user b ON a.customer_user_id=b.Id");

            if (!param.nickname.IsEmpty())
            {
                strSql.Append(" AND b.nickname like @nickname ");
                parameter.Add(DbParameterExtension.CreateDbParameter("@nickname", '%' + param.nickname + '%'));
            }

            IEnumerable<CustomerAddrEntity> list = await this.BaseRepository().FindList<CustomerAddrEntity>(strSql.ToString(), parameter.ToArray(), pagination);
            return list.ToList();
        }

        public async Task<CustomerAddrEntity> GetEntity(long id)
        {
            return await this.BaseRepository().FindEntity<CustomerAddrEntity>(id);
        }
        public async Task<CustomerAddrEntity> GetUserEntity(long userid)
        {
            return await this.BaseRepository().FindEntity<CustomerAddrEntity>(t=>t.customer_user_id== userid);
        }
        #endregion

        #region 提交数据
        public async Task SaveForm(CustomerAddrEntity entity)
        {
            if (entity.Id.IsNullOrZero())
            {
                entity.Create();
                await this.BaseRepository().Insert(entity);
            }
            else
            {
                
                await this.BaseRepository().Update(entity);
            }
        }

        public async Task DeleteForm(string ids)
        {
            long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
            await this.BaseRepository().Delete<CustomerAddrEntity>(idArr);
        }
        #endregion

        #region 私有方法
        private Expression<Func<CustomerAddrEntity, bool>> ListFilter(CustomerAddrListParam param)
        {
            var expression = LinqExtensions.True<CustomerAddrEntity>();
            if (param != null)
            {
            }
            return expression;
        }
        #endregion
    }
}
